package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.EquBindEntity;
import com.ywjh.inter.entity.InsulinPenGlucoseEntity;
import com.ywjh.inter.entity.InsulinPenInjectEntity;
import com.ywjh.inter.entity.InsulinPenInjectSetEntity;
import com.ywjh.inter.entity.InsulinPenSysMsgEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.service.DocService;
import com.ywjh.inter.service.EquBindService;
import com.ywjh.inter.service.InsulinPenGlucoseService;
import com.ywjh.inter.service.InsulinPenInjectService;
import com.ywjh.inter.service.InsulinPenInjectSetService;
import com.ywjh.inter.service.InsulinPenMedicineService;
import com.ywjh.inter.service.InsulinPenSysMsgService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class InsulinPen extends HttpServlet {

	private static final long serialVersionUID = -8213482808033813756L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		
		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");

		int r = 1;
		String e = "";
		JSONObject json = new JSONObject();
		JSONObject jsond = new JSONObject();
		switch(act) {
			case "info":
				int userId = data.getInt("user_id");
				JSONObject user = new JSONObject();
				ProfileEntity profile = UsersService.getInstance().getProfileByUserId(userId);
				if(profile != null) {
					user.put("name", profile.getName());
					user.put("gender", profile.getGender());
					user.put("birthday", profile.getBrithday());
					user.put("weight", profile.getWeight());
					user.put("disease", Optional.ofNullable(DocService.getInstance().getDiseaseName(profile.getDisease_id())).orElse(""));
				}
				jsond.put("user", user);
				
				JSONArray pens = new JSONArray();
				List<EquBindEntity> equs = EquBindService.getInstance().listByUserIdAndType(userId, 6);
				for(EquBindEntity equ: equs) {
					JSONObject pen = new JSONObject();
					pen.put("name", equ.getName());
					pen.put("sn", equ.getSn());
					InsulinPenSysMsgEntity a = InsulinPenSysMsgService.getInstance().getNewBySn(equ.getSn());
					InsulinPenGlucoseEntity b = InsulinPenGlucoseService.getInstance().getNewBySn(equ.getSn());
					InsulinPenInjectEntity c = InsulinPenInjectService.getInstance().getNewBySn(equ.getSn());

					pen.put("sys_time", a == null ? "" : a.getSysTime());
					pen.put("a", a == null ? "" : a.getA());
					pen.put("a_stu", a == null ? "" : a.getaStu());
					pen.put("bat", a == null ? "" : a.getBat());
					pen.put("glucose", b == null ? "" : b.getVlu());
					pen.put("last_vlu", c == null ? "" : c.getVlu());
					pen.put("last_time", c == null ? "" : c.getTime());
					pens.add(pen);
				}
				jsond.put("pens", pens);
				json.put("d", jsond);
				break;
			case "user_set":
				InsulinPenInjectSetEntity b = new InsulinPenInjectSetEntity();
				b.setUserId(data.getInt("user_id"));
				b.setDocId(0);
				b.setSn(data.getString("sn"));
				b.setChn("");
				b.setDrugId(data.getString("drug_id"));
				b.setAmVlu(data.getString("am_vlu"));
				b.setNoonVlu(data.getString("noon_vlu"));
				b.setPmVlu(data.getString("pm_vlu"));
				b.setBaseVlu(data.getString("base_vlu"));
				b.setTempVlu("");
				b.setDrugId2("");
				b.setType(1);
				b.setIsnew(0);
				b.setIssued(0);
				if(InsulinPenInjectSetService.getInstance().add(b)) {
					if(!InsulinPenInjectSetService.getInstance().issue(b)) {
						r = 0;
						e = "下发注射信息失败";
					}
				} else {
					r = 0;
					e = "设置注射信息失败";
				}
				break;
			case "user_set_temp":
				InsulinPenInjectSetEntity b2 = new InsulinPenInjectSetEntity();
				b2.setUserId(data.getInt("user_id"));
				b2.setDocId(0);
				b2.setSn(data.getString("sn"));
				b2.setChn("");
				b2.setDrugId(data.getString("drug_id"));
				b2.setAmVlu("");
				b2.setNoonVlu("");
				b2.setPmVlu("");
				b2.setBaseVlu("");
				b2.setTempVlu(data.getString("temp_vlu"));
				b2.setDrugId2("");
				b2.setType(1);
				b2.setIsnew(0);
				b2.setIssued(0);
				if(InsulinPenInjectSetService.getInstance().add(b2)) {
					if(!InsulinPenInjectSetService.getInstance().issue(b2)) {
						r = 0;
						e = "下发注射信息失败";
					}
				} else {
					r = 0;
					e = "设置注射信息失败";
				}
				break;
			case "doc_set":
				InsulinPenInjectSetEntity c = new InsulinPenInjectSetEntity();
				c.setUserId(data.getInt("user_id"));
				c.setDocId(data.getInt("doc_id"));
				c.setSn(data.getString("sn"));
				c.setChn("");
				c.setDrugId(data.getString("drug_id"));
				c.setAmVlu(data.getString("am_vlu"));
				c.setNoonVlu(data.getString("noon_vlu"));
				c.setPmVlu(data.getString("pm_vlu"));
				c.setBaseVlu(data.getString("base_vlu"));
				c.setTempVlu("");
				c.setDrugId2("");
				c.setType(2);
				c.setIsnew(1);
				c.setIssued(0);
				if(InsulinPenInjectSetService.getInstance().add(c)) {
					if(!InsulinPenInjectSetService.getInstance().issue(c)) {
						r = 0;
						e = "下发注射信息失败";
					}
				} else {
					r = 0;
					e = "设置注射信息失败";
				}
				break;
			case "doc_set_temp":
				InsulinPenInjectSetEntity c2 = new InsulinPenInjectSetEntity();
				c2.setUserId(data.getInt("user_id"));
				c2.setDocId(data.getInt("doc_id"));
				c2.setSn(data.getString("sn"));
				c2.setChn("");
				c2.setDrugId(data.getString("drug_id"));
				c2.setAmVlu("");
				c2.setNoonVlu("");
				c2.setPmVlu("");
				c2.setBaseVlu("");
				c2.setTempVlu(data.getString("temp_vlu"));
				c2.setDrugId2("");
				c2.setType(2);
				c2.setIsnew(1);
				c2.setIssued(0);
				if(InsulinPenInjectSetService.getInstance().add(c2)) {
					if(!InsulinPenInjectSetService.getInstance().issue(c2)) {
						r = 0;
						e = "下发注射信息失败";
					}
				} else {
					r = 0;
					e = "设置注射信息失败";
				}
				break;
			case "get_doc_set":
				userId = data.getInt("user_id");
				int page = data.getInt("page");
				int num = data.getInt("num");
				jsond.put("nums", InsulinPenInjectSetService.getInstance().getDocSetCount(userId));
				jsond.put("data", InsulinPenInjectSetService.getInstance().getDocSet(userId, page, num));
				InsulinPenInjectSetService.getInstance().setDocSetOld(userId);
				json.put("d", jsond);
				break;
			case "get_doc_set_new":
				userId = data.getInt("user_id");
				json.put("d", InsulinPenInjectSetService.getInstance().getDocSetNewCount(userId));
				break;
			case "inject":
				userId = data.getInt("user_id");
				String date = data.getString("date");
				String sn = data.getString("sn");
				json.put("d", InsulinPenInjectService.getInstance().getBySnAndTime(sn, date));
				break;
			case "glucose":
				userId = data.getInt("user_id");
				date = data.getString("date");
				sn = data.getString("sn");
				json.put("d", InsulinPenGlucoseService.getInstance().getBySnAndTime(sn, date));
				break;
			case "issue_log":
				userId = data.getInt("user_id");
				page = data.getInt("page");
				num = data.getInt("num");
				jsond.put("nums", InsulinPenInjectSetService.getInstance().getIssueLogCount(userId));
				jsond.put("data", InsulinPenInjectSetService.getInstance().getIssueLog(userId, page, num));
				json.put("d", jsond);
				break;
			case "medicine":
				json.put("d", InsulinPenMedicineService.getInstance().list());
				break;
			default:
				System.out.println("InsulinPen unknown action: " + postData);
				r = 0;
				e = "未知的act";
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
